package com.rp.configure.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rp.common.result.ResultDTO;
import com.rp.common.utils.Selectway;
import com.rp.configure.entity.Svg;
import com.rp.configure.service.SvgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vicente
 * @since 2021-07-20
 */
@RestController
@RequestMapping("/svg")
@Api("svg管理")
public class SvgController {
    @Autowired
    private SvgService svgService;
    @Autowired
    private Selectway selectway;
    @ApiOperation(value = "分页列表")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    @GetMapping(value = "/list_page")
    public ResultDTO<IPage<Svg>> listPage(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
                                          @RequestParam(required = false) @ApiParam(value = "模糊查询字符串") String string) {
        QueryWrapper<Svg> queryWrapper = new QueryWrapper<Svg>();
//        selectway.fields(queryWrapper, string,"gateway",null,null);
        queryWrapper.orderByDesc("create_time");
        return ResultDTO.success().data(svgService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
    @ApiOperation(value = "更具项目和svg_id查询单个")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    @GetMapping(value = "/one")
    public ResultDTO<Svg> list(@RequestParam(value = "subjectId") String subjectId,
                               @RequestParam(value = "userId") String userId
                               ) {
        QueryWrapper<Svg> queryWrapper = new QueryWrapper<Svg>();
//        selectway.fields(queryWrapper, string,"gateway",null,null);
        queryWrapper.eq("subject_id",subjectId).eq("user_id",userId);
        return ResultDTO.success().data(svgService.getOne(queryWrapper));
    }
    @ApiOperation(value = "根据项目和svg_id查询所属网关和设备")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    @GetMapping(value = "/get_subject")
    public ResultDTO<Svg> getBySubject(@RequestParam(value = "subjectId") String subjectId,
                               @RequestParam(value = "userId") String userId
    ) {
        QueryWrapper<Svg> queryWrapper = new QueryWrapper<Svg>();
        queryWrapper.eq("subject_id",subjectId).eq("user_id",userId);
        return ResultDTO.success().data(svgService.getOne(queryWrapper));
    }
    @PostMapping("/insert")
    @ApiOperation(value = "增加")
    @Transactional
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO insert(@RequestBody  Svg svg) {
        return ResultDTO.tf(svgService.save(svg));
    }
    @PutMapping("/update")
    @ApiOperation(value = "修改")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO update(@RequestBody Svg svg) {
        return ResultDTO.tf(svgService.updateById(svg));
    }
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO delete(@RequestBody @ApiParam(value = "id", required = true) String id) {
        return ResultDTO.tf(svgService.removeById(id));
    }
    @DeleteMapping("/deleteAll")
    @ApiOperation(value = "批量删除")
//    @RequiresRoles(value={"admin","vip"},logical = Logical.OR)
    public ResultDTO deleteAll(@RequestBody @ApiParam(value = "id(英文逗号隔开)", required = true) String ids) {
        return ResultDTO.tf(svgService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
